home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / dix / dixutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-14  |  8.0 KB  |  323 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25.  
  26. /* $XConsortium: dixutils.c,v 1.35 89/09/30 10:46:46 keith Exp $ */
  27.  
  28. #include "X.h"
  29. #include "Xmd.h"
  30. #include "misc.h"
  31. #include "window.h"
  32. #include "dixstruct.h"
  33. #include "pixmapstr.h"
  34. #include "scrnintstr.h"
  35. #define  XK_LATIN1
  36. #include "keysymdef.h"
  37.  
  38. /*
  39.  * CompareTimeStamps returns -1, 0, or +1 depending on if the first
  40.  * argument is less than, equal to or greater than the second argument.
  41.  */
  42.  
  43. int
  44. CompareTimeStamps(a, b)
  45.     TimeStamp a, b;
  46. {
  47.     if (a.months < b.months)
  48.     return EARLIER;
  49.     if (a.months > b.months)
  50.     return LATER;
  51.     if (a.milliseconds < b.milliseconds)
  52.     return EARLIER;
  53.     if (a.milliseconds > b.milliseconds)
  54.     return LATER;
  55.     return SAMETIME;
  56. }
  57.  
  58. /*
  59.  * convert client times to server TimeStamps
  60.  */
  61.  
  62. #define HALFMONTH ((unsigned long) 1<<31)
  63. TimeStamp
  64. ClientTimeToServerTime(c)
  65.      CARD32 c;
  66. {
  67.     TimeStamp ts;
  68.     if (c == CurrentTime)
  69.     return currentTime;
  70.     ts.months = currentTime.months;
  71.     ts.milliseconds = c;
  72.     if (c > currentTime.milliseconds)
  73.     {
  74.     if (((unsigned long) c - currentTime.milliseconds) > HALFMONTH)
  75.         ts.months -= 1;
  76.     }
  77.     else if (c < currentTime.milliseconds)
  78.     {
  79.     if (((unsigned long)currentTime.milliseconds - c) > HALFMONTH)
  80.         ts.months += 1;
  81.     }
  82.     return ts;
  83. }
  84.  
  85. /*
  86.  * ISO Latin-1 case conversion routine
  87.  *
  88.  * this routine always null-terminates the result, so
  89.  * beware of too-small buffers
  90.  */
  91.  
  92. void
  93. CopyISOLatin1Lowered(dest, source, length)
  94.     register unsigned char *dest, *source;
  95.     int length;
  96. {
  97.     register int i;
  98.  
  99.     for (i = 0; i < length; i++, source++, dest++)
  100.     {
  101.     if ((*source >= XK_A) && (*source <= XK_Z))
  102.         *dest = *source + (XK_a - XK_A);
  103.     else if ((*source >= XK_Agrave) && (*source <= XK_Odiaeresis))
  104.         *dest = *source + (XK_agrave - XK_Agrave);
  105.     else if ((*source >= XK_Ooblique) && (*source <= XK_Thorn))
  106.         *dest = *source + (XK_oslash - XK_Ooblique);
  107.     else
  108.         *dest = *source;
  109.     }
  110.     *dest = '\0';
  111. }
  112.  
  113. WindowPtr
  114. LookupWindow(rid, client)
  115.     XID rid;
  116.     ClientPtr client;
  117. {
  118.     client->errorValue = rid;
  119.     if(rid == INVALID)
  120.     return NULL;
  121.     if (client->lastDrawableID == rid)
  122.     {
  123.         if (client->lastDrawable->type != DRAWABLE_PIXMAP)
  124.             return ((WindowPtr) client->lastDrawable);
  125.         return (WindowPtr) NULL;
  126.     }
  127.     return (WindowPtr)LookupIDByType(rid, RT_WINDOW);
  128. }
  129.  
  130.  
  131. pointer
  132. LookupDrawable(rid, client)
  133.     XID rid;
  134.     ClientPtr client;
  135. {
  136.     register DrawablePtr pDraw;
  137.  
  138.     if(rid == INVALID)
  139.     return (pointer) NULL;
  140.     if (client->lastDrawableID == rid)
  141.     return ((pointer) client->lastDrawable);
  142.     pDraw = (DrawablePtr)LookupIDByClass(rid, RC_DRAWABLE);
  143.     if (pDraw && (pDraw->type != UNDRAWABLE_WINDOW))
  144.         return (pointer)pDraw;        
  145.     return (pointer)NULL;
  146. }
  147.  
  148.  
  149. int
  150. AlterSaveSetForClient(client, pWin, mode)
  151.     ClientPtr client;
  152.     WindowPtr pWin;
  153.     unsigned mode;
  154. {
  155.     int numnow;
  156.     pointer *pTmp;
  157.     int j;
  158.  
  159.     numnow = client->numSaved;
  160.     j = 0;
  161.     if (numnow)
  162.     {
  163.     pTmp = client->saveSet;
  164.     while ((j < numnow) && (pTmp[j] != (pointer)pWin))
  165.         j++;
  166.     }
  167.     if (mode == SetModeInsert)
  168.     {
  169.     if (j < numnow)         /* duplicate */
  170.        return(Success);
  171.     numnow++;
  172.     pTmp = (pointer *)xrealloc(client->saveSet, sizeof(pointer) * numnow);
  173.     if (!pTmp)
  174.         return(BadAlloc);
  175.     client->saveSet = pTmp;
  176.            client->numSaved = numnow;
  177.     client->saveSet[numnow - 1] = (pointer)pWin;
  178.     return(Success);
  179.     }
  180.     else if ((mode == SetModeDelete) && (j < numnow))
  181.     {
  182.     while (j < numnow-1)
  183.     {
  184.            pTmp[j] = pTmp[j+1];
  185.        j++;
  186.     }
  187.     numnow--;
  188.         if (numnow)
  189.     {
  190.             pTmp = (pointer *)xrealloc(client->saveSet,
  191.                        sizeof(pointer) * numnow);
  192.         if (pTmp)
  193.         client->saveSet = pTmp;
  194.     }
  195.         else
  196.         {
  197.             xfree(client->saveSet);
  198.         client->saveSet = (pointer *)NULL;
  199.     }
  200.     client->numSaved = numnow;
  201.     return(Success);
  202.     }
  203.     return(Success);
  204. }
  205.  
  206.  
  207. DeleteWindowFromAnySaveSet(pWin)
  208.     WindowPtr pWin;
  209. {
  210.     register int i;
  211.     register ClientPtr client;
  212.     
  213.     for (i = 0; i< currentMaxClients; i++)
  214.     {    
  215.     client = clients[i];
  216.     if (client && client->numSaved)
  217.         (void)AlterSaveSetForClient(client, pWin, SetModeDelete);
  218.     }
  219. }
  220.  
  221. /* No-op Don't Do Anything : sometimes we need to be able to call a procedure
  222.  * that doesn't do anything.  For example, on screen with only static
  223.  * colormaps, if someone calls install colormap, it's easier to have a dummy
  224.  * procedure to call than to check if there's a procedure 
  225.  */
  226. void
  227. NoopDDA()
  228. {
  229. }
  230.  
  231. typedef struct _BlockHandler {
  232.     void    (*BlockHandler)();
  233.     void    (*WakeupHandler)();
  234.     pointer blockData;
  235. } BlockHandlerRec, *BlockHandlerPtr;
  236.  
  237. static BlockHandlerPtr    handlers;
  238. static int        numHandlers;
  239. static int        sizeHandlers;
  240.  
  241. /* called from the OS layer */
  242. BlockHandler(pTimeout, pReadmask)
  243. pointer    pTimeout;    /* DIX doesn't want to know how OS represents time */
  244. pointer pReadmask;    /* nor how it represents the set of descriptors */
  245. {
  246.     register int i;
  247.     
  248.     for (i = 0; i < screenInfo.numScreens; i++)
  249.     (* screenInfo.screens[i]->BlockHandler)(i, 
  250.                 screenInfo.screens[i]->blockData,
  251.                 pTimeout, pReadmask);
  252.     for (i = 0; i < numHandlers; i++)
  253.     (*handlers[i].BlockHandler) (handlers[i].blockData,
  254.                      pTimeout, pReadmask);
  255. }
  256.  
  257.  
  258. WakeupHandler(result, pReadmask)
  259. unsigned long    result;    /* 32 bits of undefined result from the wait */
  260. pointer pReadmask;    /* the resulting descriptor mask */
  261. {
  262.     register int i;
  263.     for (i = numHandlers - 1; i >= 0; i--)
  264.     (*handlers[i].WakeupHandler) (handlers[i].blockData,
  265.                       result, pReadmask);
  266.     for (i = 0; i < screenInfo.numScreens; i++)
  267.     (* screenInfo.screens[i]->WakeupHandler)(i, 
  268.                 screenInfo.screens[i]->wakeupData,
  269.                 result, pReadmask);
  270. }
  271.  
  272. Bool
  273. RegisterBlockAndWakeupHandlers (blockHandler, wakeupHandler, blockData)
  274.     void    (*blockHandler)();
  275.     void    (*wakeupHandler)();
  276.     pointer blockData;
  277. {
  278.     BlockHandlerPtr new;
  279.  
  280.     if (numHandlers >= sizeHandlers)
  281.     {
  282.         new = (BlockHandlerPtr) xrealloc (handlers, (numHandlers + 1) *
  283.                             sizeof (BlockHandlerRec));
  284.         if (!new)
  285.         return FALSE;
  286.         handlers = new;
  287.     sizeHandlers = numHandlers + 1;
  288.     }
  289.     handlers[numHandlers].BlockHandler = blockHandler;
  290.     handlers[numHandlers].WakeupHandler = wakeupHandler;
  291.     handlers[numHandlers].blockData = blockData;
  292.     numHandlers = numHandlers + 1;
  293.     return TRUE;
  294. }
  295.  
  296. void
  297. RemoveBlockAndWakeupHandlers (blockHandler, wakeupHandler, blockData)
  298.     void    (*blockHandler)();
  299.     void    (*wakeupHandler)();
  300.     pointer blockData;
  301. {
  302.     int        i;
  303.  
  304.     for (i = 0; i < numHandlers; i++)
  305.     if (handlers[i].BlockHandler == blockHandler &&
  306.         handlers[i].WakeupHandler == wakeupHandler &&
  307.         handlers[i].blockData == blockData)
  308.     {
  309.         for (; i < numHandlers - 1; i++)
  310.         handlers[i] = handlers[i+1];
  311.         numHandlers--;
  312.         break;
  313.     }
  314. }
  315.  
  316. InitBlockAndWakeupHandlers ()
  317. {
  318.     xfree (handlers);
  319.     handlers = (BlockHandlerPtr) 0;
  320.     numHandlers = 0;
  321.     sizeHandlers = 0;
  322. }
  323.